<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Http\Model\User;
use App\Http\Model\Goods;
use App\Http\Model\Menu;
use App\Http\Model\OrdersInfo;
use DB;
use Excel;

class ReportController extends Controller
{
    public function performanceReport(Request $request){

        $start = $request->input("start", 0);
        $end = $request->input("end", 0);
        $type = $request->input("type", 0);
        $menu = new Menu;
        $menuInfo = $menu->get();
        $goodsInfo = $this->getPerformanceData($start,$end,$type);
        return view("admin.report.performance-report", ["goodsInfo"=>$goodsInfo,"start"=>$start, "end"=>$end, "type"=>$type, "menuInfo"=>$menuInfo]);
    }
    public function getPerformanceData($start,$end,$type){
        $user = new User;
        $orders = new OrdersInfo;
        if($type == 0){
            if($start == 0 && $end == 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->where("orders.status", ">", "3")->get()->toArray();
            }else if($start == 0 && $end != 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->where("orders_info.updated_at",">",$start." 00:00:00")->where("orders.status", ">", "3")->get()->toArray();
            }else if($start != 0 && $end == 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->where("orders_info.updated_at","<",$end." 23:59:59")->where("orders.status", ">", "3")->get()->toArray();
            }else{
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->where("orders_info.updated_at",">",$start." 00:00:00")->where("orders_info.updated_at","<",$end." 23:59:59")->where("orders.status", ">", "3")->get()->toArray();
            }
        }else{
            if($start == 0 && $end == 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->leftJoin("goods", "goods.id", "=", "goods_id")->where("menu_id",$type)->where("orders.status", ">", "3")->get()->toArray();
            }else if($start == 0 && $end != 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->leftJoin("goods", "goods.id", "=", "goods_id")->where("menu_id",$type)->where("orders_info.updated_at",">",$start." 00:00:00")->where("orders.status", ">", "3")->get()->toArray();
            }else if($start != 0 && $end == 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->leftJoin("goods", "goods.id", "=", "goods_id")->where("menu_id",$type)->where("orders_info.updated_at","<",$end." 23:59:59")->where("orders.status", ">", "3")->get()->toArray();
            }else{
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->leftJoin("goods", "goods.id", "=", "goods_id")->where("menu_id",$type)->where("orders_info.updated_at",">",$start." 00:00:00")->where("orders_info.updated_at","<",$end." 23:59:59")->where("orders.status", ">", "3")->get()->toArray();
            }
        }
        $res1 = array();
        foreach($ordersInfo as $k=>$v){
            $res1[$v['goods_id']]["goods_name"] = $v["goods_name"];
            $res1[$v['goods_id']]["goods_spec"] = $v["goods_spec"];
            $res1[$v['goods_id']]["ordersInfo"][]=$v;
        }
        // dd($res1);
        foreach($res1 as $k=>$v){
            $res1[$k]["sum"] = count($v["ordersInfo"]);
        }
        // dd($res1);
        $res2 = array();
        foreach($res1 as $k=>$v){
            $res2[$k]["goods_name"] = $res1[$k]["goods_name"];
            $res2[$k]["goods_spec"] = $res1[$k]["goods_spec"];
            $res2[$k]["sum"] = $res1[$k]["sum"];
            foreach($v["ordersInfo"] as $kk=>$vv){
                $res2[$k]["ordersInfo"][$vv['clothing_attr_id']]["ordersInfo"][]=$vv;
            }
        }
        foreach($res2 as $k=>$v){
            $res2[$k]["sum"] = 0;
            $res2[$k]["sumNum"] = 0;
            $res2[$k]["sumPrice"] = 0;
            foreach($v["ordersInfo"] as $kk=>$vv){
                $res2[$k]["ordersInfo"][$kk]["sumNum"] = 0;
                $res2[$k]["ordersInfo"][$kk]["sumPrice"] = 0;
                foreach($vv["ordersInfo"] as $kkk=>$vvv){
                    $res2[$k]["ordersInfo"][$kk]["spec"] = $vvv["spec"];
                    $res2[$k]["ordersInfo"][$kk]["sumNum"] += $vvv["num"];
                    $res2[$k]["ordersInfo"][$kk]["sumPrice"] += $vvv["num"]*$vvv["price"];
                }
                $res2[$k]["sumNum"] += $res2[$k]["ordersInfo"][$kk]["sumNum"];
                $res2[$k]["sumPrice"] += $res2[$k]["ordersInfo"][$kk]["sumPrice"];
            }
            $res2[$k]["sum"] += count($res2[$k]["ordersInfo"]);
        }
        ksort($res2);
        // dd($res2);
        return $res2;
    }
    public function exportPerformanceExcel($start=0,$end=0,$type=0){
        $menu = new Menu;
        $goodsInfo = $this->getPerformanceData($start,$end,$type);
        $title = "从";
        if($start != 0){
            $title .= $start."～";
        }else{
            $title .= "开始营业～";
        }
        if($end != 0){
            $title .= $end;
        }else{
            $title .= date("Y-m-d");
        }
        if($type != 0){
            $title .= $menu->find($type)->name."业绩报表";
        }else{
            $title .= "全部业绩报表";
        }
        Excel::create(iconv('UTF-8', 'GBK', $title),function($excel) use ($goodsInfo, $title){
            $excel->sheet('goods', function($sheet) use ($goodsInfo, $title){
                $num = 4;
                $sheet->mergeCells('A1:H1');
                $sheet->setHeight(1, 30);
                $sheet->cells("A1:F1", function($cells) {
                    $cells->setFontSize(12);
                });
                $sheet->row(1, array(
                     $title
                ));
                $sheet->mergeCells('F2:H2');
                $sheet->setMergeColumn(array(
                    'columns' => array('A', 'B', 'C', 'D', 'E'),
                    'rows' => array(
                        array(2,3),
                    )
                ));
                $sheet->row(2, array(
                     'ID', '商品名称', '规格', '总量', '总价', '购买属性'
                ));
                $sheet->row(3, array(
                     'ID', '商品名称', '规格', '总量', '总价', '购买规格', '购买数量', '购买总价（元）'
                ));
                $totalNum = 0;
                $totalPrice = 0;
                $id = 1;
                foreach ($goodsInfo as $kk => $vv) {
                    $sum = $vv["sum"];
                    if($sum != 0){
                        $end = $num+$sum-1;
                        $sheet->setMergeColumn(array(
                            'columns' => array('A','B','C','D','E'),
                            'rows' => array(
                                array($num,$end)
                            )
                        ));
                        foreach ($vv["ordersInfo"] as $kkk => $vvv) {
                            $totalNum += $vvv["sumNum"];
                            $totalPrice += $vvv["sumPrice"];
                            $sheet->row($num, array(
                                 $id, $vv["goods_name"], $vv["goods_spec"], $vv["sumNum"], $vv["sumPrice"], $vvv["spec"], $vvv["sumNum"], $vvv["sumPrice"]
                            ));
                            $num++;
                        }
                        $id++;
                    }
                }
                $sheet->row($num, array(
                     "合计", "", "", "", "", "", $totalNum, $totalPrice
                ));
                $sheet->setHeight($num, 30);
                $sheet->setWidth(array(
                    'A'     =>  10,
                    'B'     =>  40,
                    'C'     =>  20,
                    'D'     =>  20,
                    'E'     =>  40,
                    'F'     =>  40,
                    'G'     =>  20,
                    'H'     =>  40
                ));
                $sheet->cells("A$num:H$num", function($cells) {
                    $cells->setFontSize(12);
                    $cells->setFontColor('#DC143C');
                    $cells->setFontWeight('bold');
                });
                $sheet->cells("A$num", function($cells) {
                    $cells->setFontSize(10);
                });
                $sheet->cells("A1:H$num", function($cells) {
                    $cells->setAlignment('center');
                    $cells->setValignment('center');
                });
            });
        })->export('xls');
    }
    public function userReport(Request $request){
        $start = $request->input("start", 0);
        $end = $request->input("end", 0);
        $type = $request->input("type", 0);
        $menu = new Menu;
        $menuInfo = $menu->get();
        $goodsInfo = $this->getUserData($start,$end,$type);
        // dd($goodsInfo);
        return view("admin.report.user-report", ["goodsInfo"=>$goodsInfo,"start"=>$start, "end"=>$end, "type"=>$type, "menuInfo"=>$menuInfo]);
    }
    public function getUserData($start,$end,$type){
        $user = new User;
        $orders = new OrdersInfo;
        if($type == 0){
            if($start == 0 && $end == 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->where("orders.status", ">", "3")->get()->toArray();
            }else if($start == 0 && $end != 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->where("orders_info.updated_at",">",$start." 00:00:00")->where("orders.status", ">", "3")->get()->toArray();
            }else if($start != 0 && $end == 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->where("orders_info.updated_at","<",$end." 23:59:59")->where("orders.status", ">", "3")->get()->toArray();
            }else{
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->where("orders_info.updated_at",">",$start." 00:00:00")->where("orders_info.updated_at","<",$end." 23:59:59")->where("orders.status", ">", "3")->get()->toArray();
            }
        }else{
            if($start == 0 && $end == 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->leftJoin("goods", "goods.id", "=", "goods_id")->where("menu_id",$type)->where("orders.status", ">", "3")->get()->toArray();
            }else if($start == 0 && $end != 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->leftJoin("goods", "goods.id", "=", "goods_id")->where("menu_id",$type)->where("orders_info.updated_at",">",$start." 00:00:00")->where("orders.status", ">", "3")->get()->toArray();
            }else if($start != 0 && $end == 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->leftJoin("goods", "goods.id", "=", "goods_id")->where("menu_id",$type)->where("orders_info.updated_at","<",$end." 23:59:59")->where("orders.status", ">", "3")->get()->toArray();
            }else{
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->leftJoin("goods", "goods.id", "=", "goods_id")->where("menu_id",$type)->where("orders_info.updated_at",">",$start." 00:00:00")->where("orders_info.updated_at","<",$end." 23:59:59")->where("orders.status", ">", "3")->get()->toArray();
            }
        }
        $res1 = array();
        foreach($ordersInfo as $k=>$v){
            $res1[$v['user_id']]["username"] = $user->find($v["user_id"])->username;
            $res1[$v['user_id']]["ordersInfo"][]=$v;
        }
        // dd($res1);
        $res2 = array();
        foreach($res1 as $k=>$v){
            $res2[$k]["username"] = $res1[$k]["username"];
            foreach($v["ordersInfo"] as $kk=>$vv){
                $res2[$k]["ordersInfo"][$vv['goods_id']]["goods_name"] = $vv["goods_name"];
                $res2[$k]["ordersInfo"][$vv['goods_id']]["goods_spec"] = $vv["goods_spec"];
                $res2[$k]["ordersInfo"][$vv['goods_id']]["ordersInfo"][]=$vv;
            }
        }
        // dd($res2);
        $res3 = array();
        foreach($res2 as $k=>$v){
            $res3[$k]["username"] = $res2[$k]["username"];
            foreach($v["ordersInfo"] as $kk=>$vv){
                $res3[$k]["ordersInfo"][$kk]["goods_name"] = $res2[$k]["ordersInfo"][$kk]["goods_name"];
                $res3[$k]["ordersInfo"][$kk]["goods_spec"] = $res2[$k]["ordersInfo"][$kk]["goods_spec"];
                foreach($vv["ordersInfo"] as $kkk=>$vvv){
                    $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$vvv['clothing_attr_id']]["ordersInfo"][]=$vvv;
                }
            }
        }
        // dd($res3);
        foreach($res3 as $k=>$v){
            $res3[$k]["sum"] = 0;
            $res3[$k]["sumNum"] = 0;
            $res3[$k]["sumPrice"] = 0;
            foreach($v["ordersInfo"] as $kk=>$vv){
                $res3[$k]["ordersInfo"][$kk]["sum"] = 0;
                $res3[$k]["ordersInfo"][$kk]["sumNum"] = 0;
                $res3[$k]["ordersInfo"][$kk]["sumPrice"] = 0;
                foreach($vv["ordersInfo"] as $kkk=>$vvv){
                    $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["sumNum"] = 0;
                    $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["sumPrice"] = 0;
                    foreach ($vvv["ordersInfo"] as $kkkk => $vvvv) {
                        $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["spec"] = $vvvv["spec"];
                        $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["sumNum"] += $vvvv["num"];
                        $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["sumPrice"] += $vvvv["num"]*$vvvv["price"];
                    }
                    $res3[$k]["ordersInfo"][$kk]["sumNum"] += $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["sumNum"];
                    $res3[$k]["ordersInfo"][$kk]["sumPrice"] += $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["sumPrice"];
                }
                $res3[$k]["ordersInfo"][$kk]["sum"] += count($res3[$k]["ordersInfo"][$kk]["ordersInfo"]);
                $res3[$k]["sum"] += count($res3[$k]["ordersInfo"][$kk]["ordersInfo"]);
                $res3[$k]["sumNum"] += $res3[$k]["ordersInfo"][$kk]["sumNum"];
                $res3[$k]["sumPrice"] += $res3[$k]["ordersInfo"][$kk]["sumPrice"];
            }
        }
        // dd($res3);
        ksort($res3);
        return $res3;
    }
    public function exportUserExcel($start=0,$end=0,$type=0){
        $menu = new Menu;
        $goodsInfo = $this->getUserData($start,$end,$type);
        $title = "从";
        if($start != 0){
            $title .= $start."～";
        }else{
            $title .= "开始营业～";
        }
        if($end != 0){
            $title .= $end;
        }else{
            $title .= date("Y-m-d");
        }
        if($type != 0){
            $title .= $menu->find($type)->name."用户报表";
        }else{
            $title .= "全部用户报表";
        }
        Excel::create(iconv('UTF-8', 'GBK', $title),function($excel) use ($goodsInfo, $title){
            $excel->sheet('user', function($sheet) use ($goodsInfo, $title){
                $num1 = 4;
                $num2 = 4;
                $sheet->mergeCells('A1:K1');
                $sheet->setHeight(1, 30);
                $sheet->cells("A1:K1", function($cells) {
                    $cells->setFontSize(12);
                });
                $sheet->row(1, array(
                     $title
                ));
                $sheet->mergeCells('I2:K2');
                $sheet->setMergeColumn(array(
                    'columns' => array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'),
                    'rows' => array(
                        array(2,3),
                    )
                ));
                $sheet->row(2, array(
                     'ID', '用户名', '总量', '总价', '商品名称', '规格', '规格总量', '规格总价', '购买属性'
                ));
                $sheet->row(3, array(
                     'ID', '用户名', '总量', '总价', '商品名称', '规格', '规格总量', '规格总价', '购买规格', '购买数量', '购买总价（元）'
                ));
                $totalNum = 0;
                $totalPrice = 0;
                $id = 1;
                foreach ($goodsInfo as $kk => $vv) {
                    $sum1 = $vv["sum"];
                    if($sum1 != 0){
                        $end1 = $num1 + $sum1 - 1;
                        $sheet->setMergeColumn(array(
                            'columns' => array('A','B','C','D'),
                            'rows' => array(
                                array($num1,$end1)
                            )
                        ));
                        foreach ($vv["ordersInfo"] as $kkk => $vvv) {
                            foreach ($vvv["ordersInfo"] as $kkkk => $vvvv) {
                                foreach ($vvvv["ordersInfo"] as $kkkkk => $vvvvv) {
                                    $sum2 = $vvv["sum"];
                                    if($sum2 != 0){
                                        $end2 = $num2 + $sum2 - 1;
                                        $sheet->setMergeColumn(array(
                                            'columns' => array('E','F','G','H'),
                                            'rows' => array(
                                                array($num2,$end2)
                                            )
                                        ));
                                    }
                                }
                                $sheet->row($num2, array(
                                    $id, $vv["username"], $vv["sumNum"], $vv["sumPrice"], $vvv["goods_name"], $vvv["goods_spec"], $vvv["sumNum"], $vvv["sumPrice"], $vvvv["spec"], $vvvv["sumNum"], $vvvv["sumPrice"]
                                ));
                                $totalNum += $vvvv["sumNum"];
                                $totalPrice += $vvvv["sumPrice"];
                                $num1++;
                                $num2++;
                            }
                        }
                    }
                    $id++;
                }
                $sheet->row($num1, array(
                     "合计", "", "", "", "", "", "", "", "", $totalNum, $totalPrice
                ));
                $sheet->setHeight($num1, 30);
                $sheet->setWidth(array(
                    'A'     =>  10,
                    'B'     =>  20,
                    'C'     =>  20,
                    'D'     =>  40,
                    'E'     =>  40,
                    'F'     =>  20,
                    'G'     =>  20,
                    'H'     =>  40,
                    'I'     =>  40,
                    'J'     =>  20,
                    'K'     =>  40
                ));
                $sheet->cells("A$num1:K$num1", function($cells) {
                    $cells->setFontSize(12);
                    $cells->setFontColor('#DC143C');
                    $cells->setFontWeight('bold');
                });
                $sheet->cells("A$num1", function($cells) {
                    $cells->setFontSize(10);
                });
                $sheet->cells("A1:K$num1", function($cells) {
                    $cells->setAlignment('center');
                    $cells->setValignment('center');
                });
            });
        })->export('xls');
    }
    public function saleReport(Request $request){
        $start = $request->input("start", 0);
        $end = $request->input("end", 0);
        $type = $request->input("type", 0);
        $menu = new Menu;
        $menuInfo = $menu->get();
        $goodsInfo = $this->getSaleData($start,$end,$type);
        return view("admin.report.sale-report", ["goodsInfo"=>$goodsInfo,"start"=>$start, "end"=>$end, "type"=>$type, "menuInfo"=>$menuInfo]);
    }
    public function getSaleData($start,$end,$type){
        $user = new User;
        $orders = new OrdersInfo;
        if($type == 0){
            if($start == 0 && $end == 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->where("orders.status", ">", "3")->get()->toArray();
            }else if($start == 0 && $end != 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->where("orders_info.updated_at",">",$start." 00:00:00")->where("orders.status", ">", "3")->get()->toArray();
            }else if($start != 0 && $end == 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->where("orders_info.updated_at","<",$end." 23:59:59")->where("orders.status", ">", "3")->get()->toArray();
            }else{
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->where("orders_info.updated_at",">",$start." 00:00:00")->where("orders_info.updated_at","<",$end." 23:59:59")->where("orders.status", ">", "3")->get()->toArray();
            }
        }else{
            if($start == 0 && $end == 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->leftJoin("goods", "goods.id", "=", "goods_id")->where("menu_id",$type)->where("orders.status", ">", "3")->get()->toArray();
            }else if($start == 0 && $end != 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->leftJoin("goods", "goods.id", "=", "goods_id")->where("menu_id",$type)->where("orders_info.updated_at",">",$start." 00:00:00")->where("orders.status", ">", "3")->get()->toArray();
            }else if($start != 0 && $end == 0){
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->leftJoin("goods", "goods.id", "=", "goods_id")->where("menu_id",$type)->where("orders_info.updated_at","<",$end." 23:59:59")->where("orders.status", ">", "3")->get()->toArray();
            }else{
                $ordersInfo = $orders->select(DB::raw("orders_info.*,orders.user_id"))->leftJoin("orders", "orders.id", "=", "orders_id")->leftJoin("goods", "goods.id", "=", "goods_id")->where("menu_id",$type)->where("orders_info.updated_at",">",$start." 00:00:00")->where("orders_info.updated_at","<",$end." 23:59:59")->where("orders.status", ">", "3")->get()->toArray();
            }
        }
        if(count($ordersInfo) != 0){
            $res1 = array();
            // dd($ordersInfo);
            foreach($ordersInfo as $k=>$v){
                $pid = $user->find($v["user_id"])->pid;
                if($pid != 0){
                    $res1[$pid]["ordersInfo"][$v['user_id']]["username"] = $user->find($v["user_id"])->username;
                    $res1[$pid]["pusername"] = $user->find($pid)->username;
                    $res1[$pid]["ordersInfo"][$v['user_id']]["ordersInfo"][]=$v;
                }else{
                    unset($res1[$pid]["ordersInfo"][$v['user_id']]);
                }
            }
            // dd($res1);
            $res2 = array();
            foreach ($res1 as $k => $v) {
                foreach($v["ordersInfo"] as $kk=>$vv){
                    $res2[$k]["ordersInfo"][$kk]["username"] = $res1[$k]["ordersInfo"][$kk]["username"];
                    $res2[$k]["pusername"] = $res1[$k]["pusername"];
                    foreach($vv["ordersInfo"] as $kkk=>$vvv){
                        $res2[$k]["ordersInfo"][$kk]["ordersInfo"][$vvv['goods_id']]["goods_name"] = $vvv["goods_name"];
                        $res2[$k]["ordersInfo"][$kk]["ordersInfo"][$vvv['goods_id']]["goods_spec"] = $vvv["goods_spec"];
                        $res2[$k]["ordersInfo"][$kk]["ordersInfo"][$vvv['goods_id']]["ordersInfo"][]=$vvv;
                    }
                }
            }
            // dd($res2);
            $res3 = array();
            foreach($res2 as $k=>$v){
                foreach($v["ordersInfo"] as $kk=>$vv){
                    $res3[$k]["ordersInfo"][$kk]["username"] = $res2[$k]["ordersInfo"][$kk]["username"];
                    $res3[$k]["pusername"] = $res2[$k]["pusername"];
                    foreach($vv["ordersInfo"] as $kkk=>$vvv){
                        $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["goods_name"] = $res2[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["goods_name"];
                        $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["goods_spec"] = $res2[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["goods_spec"];
                        foreach($vvv["ordersInfo"] as $kkkk=>$vvvv){
                            $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["ordersInfo"][$vvvv['clothing_attr_id']]["ordersInfo"][]=$vvvv;
                        }
                    }
                }
            }
            // dd($res3);
            foreach($res3 as $k=>$v){
                $res3[$k]["sum"] = 0;
                $res3[$k]["sumNum"] = 0;
                $res3[$k]["sumPrice"] = 0;
                foreach($v["ordersInfo"] as $kk=>$vv){
                    $res3[$k]["ordersInfo"][$kk]["sum"] = 0;
                    $res3[$k]["ordersInfo"][$kk]["sumNum"] = 0;
                    $res3[$k]["ordersInfo"][$kk]["sumPrice"] = 0;
                    foreach($vv["ordersInfo"] as $kkk=>$vvv){
                        $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["sum"] = 0;
                        $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["sumNum"] = 0;
                        $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["sumPrice"] = 0;
                        foreach($vvv["ordersInfo"] as $kkkk=>$vvvv){
                            $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["ordersInfo"][$kkkk]["sumNum"] = 0;
                            $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["ordersInfo"][$kkkk]["sumPrice"] = 0;
                            foreach ($vvvv["ordersInfo"] as $kkkkk => $vvvvv) {
                                $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["ordersInfo"][$kkkk]["spec"] = $vvvvv["spec"];
                                $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["ordersInfo"][$kkkk]["sumNum"] += $vvvvv["num"];
                                $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["ordersInfo"][$kkkk]["sumPrice"] += $vvvvv["num"]*$vvvvv["price"];
                            }
                            $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["sumNum"] += $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["ordersInfo"][$kkkk]["sumNum"];
                            $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["sumPrice"] += $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["ordersInfo"][$kkkk]["sumPrice"];
                        }
                        $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["sum"] += count($res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["ordersInfo"]);
                        $res3[$k]["ordersInfo"][$kk]["sum"] += count($res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["ordersInfo"]);
                        $res3[$k]["sum"] += count($res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["ordersInfo"]);
                        $res3[$k]["ordersInfo"][$kk]["sumNum"] += $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["sumNum"];
                        $res3[$k]["ordersInfo"][$kk]["sumPrice"] += $res3[$k]["ordersInfo"][$kk]["ordersInfo"][$kkk]["sumPrice"];
                    }
                    $res3[$k]["sumNum"] += $res3[$k]["ordersInfo"][$kk]["sumNum"];
                    $res3[$k]["sumPrice"] += $res3[$k]["ordersInfo"][$kk]["sumPrice"];
                }
            }
        }
        ksort($res3);
        return $res3;
    }
    public function exportSaleExcel($start=0,$end=0,$type=0){
        $menu = new Menu;
        $goodsInfo = $this->getSaleData($start,$end,$type);
        $title = "从";
        if($start != 0){
            $title .= $start."～";
        }else{
            $title .= "开始营业～";
        }
        if($end != 0){
            $title .= $end;
        }else{
            $title .= date("Y-m-d");
        }
        if($type != 0){
            $title .= $menu->find($type)->name."销量报表";
        }else{
            $title .= "全部销量报表";
        }
        Excel::create(iconv('UTF-8', 'GBK', $title),function($excel) use ($goodsInfo, $title){
            $excel->sheet('user', function($sheet) use ($goodsInfo, $title){
                $num1 = 4;
                $num2 = 4;
                $num3 = 4;
                $sheet->mergeCells('A1:N1');
                $sheet->setHeight(1, 30);
                $sheet->cells("A1:N1", function($cells) {
                    $cells->setFontSize(12);
                });
                $sheet->row(1, array(
                     $title
                ));
                $sheet->mergeCells('L2:N2');
                $sheet->setMergeColumn(array(
                    'columns' => array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'),
                    'rows' => array(
                        array(2,3),
                    )
                ));
                $sheet->row(2, array(
                    'ID', '用户名', '团队总量', '团队总价', '团队', '商品总量', '商品总价', '商品名称', '规格', '规格总量', '规格总价', '商品属性'
                ));
                $sheet->row(3, array(
                    'ID', '用户名', '团队总量', '团队总价', '团队', '商品总量', '商品总价', '商品名称', '规格', '规格总量', '规格总价', '购买规格', '购买数量', '购买总价（元）'
                ));
                $totalNum = 0;
                $totalPrice = 0;
                $id = 1;
                foreach ($goodsInfo as $kk => $vv) {
                    $sum1 = $vv["sum"];
                    if($sum1 != 0){
                        $end1 = $num1 + $sum1 - 1;
                        $sheet->setMergeColumn(array(
                            'columns' => array('A', 'B', 'C', 'D'),
                            'rows' => array(
                                array($num1,$end1)
                            )
                        ));
                        foreach ($vv["ordersInfo"] as $kkk => $vvv) {
                            $sum2 = $vvv["sum"];
                            if($sum2 != 0){
                                $end2 = $num2 + $sum2 - 1;
                                $sheet->setMergeColumn(array(
                                    'columns' => array('E', 'F', 'G'),
                                    'rows' => array(
                                        array($num2,$end2)
                                    )
                                ));
                                foreach ($vvv["ordersInfo"] as $kkkk => $vvvv) {
                                    foreach ($vvvv["ordersInfo"] as $kkkkk => $vvvvv) {
                                        foreach ($vvvvv["ordersInfo"] as $kkkkkk => $vvvvvv){
                                            $sum3 = $vvvv["sum"];
                                            if($sum3 != 0){
                                                $end3 = $num3 + $sum3 - 1;
                                                $sheet->setMergeColumn(array(
                                                    'columns' => array('H', 'I', 'J', 'K'),
                                                    'rows' => array(
                                                        array($num3,$end3)
                                                    )
                                                ));
                                            }
                                        }
                                        $sheet->row($num3, array(
                                            $id, $vv["pusername"], $vv["sumNum"], $vv["sumPrice"], $vvv["username"], $vvv["sumNum"], $vvv["sumPrice"], $vvvv["goods_name"], $vvvv["goods_spec"], $vvvv["sumNum"], $vvvv["sumPrice"], $vvvvv["spec"], $vvvvv["sumNum"], $vvvvv["sumPrice"]
                                        ));
                                        $totalNum += $vvvvv["sumNum"];
                                        $totalPrice += $vvvvv["sumPrice"];
                                        $num1++;
                                        $num2++;
                                        $num3++;
                                    }
                                }
                            }
                        }
                    }
                    $id++;
                }
                $sheet->row($num1, array(
                     "合计", "", "", "", "", "", "", "", "", "", "", "", $totalNum, $totalPrice
                ));
                $sheet->setHeight($num1, 30);
                $sheet->setWidth(array(
                    'A'     =>  10,
                    'B'     =>  20,
                    'C'     =>  20,
                    'D'     =>  40,
                    'E'     =>  20,
                    'F'     =>  20,
                    'G'     =>  40,
                    'H'     =>  40,
                    'I'     =>  20,
                    'J'     =>  20,
                    'K'     =>  40,
                    'L'     =>  40,
                    'M'     =>  20,
                    'N'     =>  40
                ));
                $sheet->cells("A$num1:N$num1", function($cells) {
                    $cells->setFontSize(12);
                    $cells->setFontColor('#DC143C');
                    $cells->setFontWeight('bold');
                });
                $sheet->cells("A$num1", function($cells) {
                    $cells->setFontSize(10);
                });
                $sheet->cells("A1:N$num1", function($cells) {
                    $cells->setAlignment('center');
                    $cells->setValignment('center');
                });
            });
        })->export('xls');
    }
}